﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Admin.Areas.Administration.Models;
using System.Web.Security;
using Admin.Common;
using Admin.Models;
using System.Data.Entity.Validation;

namespace Admin.Areas.Administration.Controllers
{
    
    public class ManageUserController : AdminBaseController
    {
        //
        // GET: /Admin/ManageUser/
        [AllowAnonymous]
        [HttpGet]
        public ActionResult LogIn()
        {
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult LogIn(LoginModel userModel)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(userModel.UserName, userModel.Password))
                {
                    FormsAuthentication.SetAuthCookie(userModel.UserName, true);
                         return RedirectToAction("Index", "Home");
                }
            }
            return RedirectToAction("Login", "ManageUser");
        }

        public ActionResult LogOut()
        {
            Session.Clear();
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

        [HttpGet]
        public ActionResult CreateUseResult()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateUseResult(CreateUserModel userModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var db = new BookingDBEntities();
                      
                    MembershipCreateStatus createStatus;

                    Membership.CreateUser(userModel.UserName, userModel.Password, userModel.Email,
                        passwordQuestion: null, passwordAnswer: null, isApproved: false, providerUserKey: null,
                        status: out createStatus);

                    if (createStatus == MembershipCreateStatus.Success)
                    {
                        var user = new TblUserProfile
                        {
                            UserName = userModel.UserName,
                            Fullname = userModel.FullName,
                            Phone = userModel.Phone,
                            Email = userModel.Email,
                            IsConfirm = userModel.IsConfirm,
                            Address = userModel.Address

                        };

                        db.TblUserProfiles.Add(user);
                        db.SaveChanges();

                        if (userModel.IsConfirm)
                        {
                            MembershipUser memUserShip;
                            memUserShip = Membership.GetUser(userModel.UserName);
                            memUserShip.IsApproved = true;
                            Membership.UpdateUser(memUserShip);
                        }

                        if (!Roles.RoleExists("HotelManager"))
                        {
                            Roles.AddUserToRole(userModel.UserName, "HotelManager");
                        }
                    }
                }
                return View();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }


        public ActionResult EditUser(string username)
        {
            var db = new BookingDBEntities();
          
            TblUserProfile user = db.TblUserProfiles.Single(m => m.UserName == "matngua");
            return View(user);

        }

        [HttpPost]
        public ActionResult EditUser(CreateUserModel userModel)
        {
            return View();
        }

        public ViewResult ListHotelManager() {

            var listHotelManage = new List<ModelViewHotelManager>();
            var db = new BookingDBEntities();
          

            foreach (var item in db.TblUserProfiles.ToList())
            {
                ModelViewHotelManager temp = new ModelViewHotelManager
                {
                    UserName = item.UserName,
                    Email = item.Email,
                    Address = item.Address,
                    Fullname = item.Fullname,
                    IsConfirm = (bool)item.IsConfirm,
                    Phone = item.Phone

                };
                listHotelManage.Add(temp);
            }

            ViewBag.ListHotelManage = listHotelManage;

            return View();
        }
    }
}
